//
//  ST_RecommendVIdeoBottomView.swift
//  btvideo
//
//  Created by mac on 2020/6/8.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit

class ST_RecommendVIdeoBottomView: UIView {
    
    static let height = BTSafeAreaBottom + 37
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .clear
        
        self.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 0, left: 0, bottom: BTSafeAreaBottom, right: 0))
        }
        
        root_view.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(36)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15)
        }
        
        
        root_view.addSubview(inputBtn)
        
        inputBtn.snp.makeConstraints { (make) in
            
            make.centerY.equalTo(avatar_image_view)
            make.left.equalTo(avatar_image_view.snp.right).offset(10)
            make.right.equalToSuperview().offset(-74)
            make.height.equalTo(36)
            
        }
        
        root_view.addSubview(anonymity_label)
        anonymity_label.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalTo(inputBtn)
            make.height.equalTo(16)
        }
        
        root_view.addSubview(select_control)
        select_control.snp.makeConstraints { (make) in
            make.centerX.equalTo(anonymity_label)
            make.bottom.equalTo(anonymity_label.snp.top).offset(11/2.0)
        }
        
        settingControlData()       
        
        
        
    }
    
    @objc fileprivate func anonymous_status_observer(){
        settingControlData()
    }
    
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        if newSuperview != nil{
            
            NotificationCenter.default.addObserver(self, selector: #selector(anonymous_status_observer), name: Notification.Name.anonymousStatus, object: nil)
            
//            print("添加奇怪通知")
            
        }else{
            
//            print("移除奇怪通知")
            
            NotificationCenter.default.removeObserver(self)
        }
        
    }
    
    
    func settingControlData(){
        
        guard let attributes = ST_BTUserInfoManager.shared.additional_attributes else {
            return
        }
        
        self.select_control.isOn = attributes.anonymousRemark
        
        if attributes.anonymousRemark{
            
            avatar_image_view.image = ST_ImageUtils.imageName("anonymous_small_icon")
            
            inputBtn.setTitleForAllStates("只有发布者本人能看到你的头像")
            
            return
        }
        
        inputBtn.setTitleForAllStates("请输入评论")
        
        if let url  = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.avatar.compress_small_image(width: 40*3).url{
            
            avatar_image_view.pin_setImage(from: url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //    MARK: - Properties
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = .clear
        return control
    }()
    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 36/2.0
        control.borderColor = UIColor.white
        control.borderWidth = 1
        return control
    }()
    
    
    fileprivate lazy var inputBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(white: 0, alpha: 0.3)
        control.cornerRadius = 36/2.0
        control.setTitleForAllStates("请输入评论")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 0)
        control.contentHorizontalAlignment = .left
        control.setTitleColor(UIColor.init(hex: 0x7B8089)!, for: UIControl.State.normal)
        control.addTarget(self, action: #selector(inputHandle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var select_control:UISwitch = {
        let control = UISwitch()
        control.onTintColor = UIColor.init(hex: 0xFEC207)
        control.tintColor = UIColor.init(hex: 0xACB4C0)
        control.backgroundColor = UIColor.init(hex: 0xACB4C0)
        control.layer.cornerRadius = 14
        control.isOn = ST_BTUserInfoManager.shared.additional_attributes?.anonymousRemark ?? false
        control.transform = CGAffineTransform(scaleX: 20/31.0, y: 20/31.0)
        control.addTarget(self, action: #selector(_switch_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    @objc fileprivate func _switch_handle(){
        
        
        if select_control.isOn{
            
            self.select_control.isOn = false
            
            let vc = BTVerticalAlertViewController.createAnonymousCommentAlert()
            
            vc.confirm_closure = {
                
                JVrequest(PH.setPrivacyValueApi,parameters: ["is_anonymous": "1"], success: { (json) in
                    
                    ST_BTUserInfoManager.shared.additional_attributes?.anonymousRemark = true
                    
                }) { (code, err) in
                    
                    
                    self.parentViewController?.toast(text: err)
                    
                }
                
            }
            
            GlobalPresent(toController: vc)
            
        }else{
            
            JVrequest(PH.setPrivacyValueApi,parameters: ["is_anonymous": "2"], success: { (json) in
                
                ST_BTUserInfoManager.shared.additional_attributes?.anonymousRemark = false
                
            }) { (code, err) in
                
                self.select_control.isOn = true
                
                self.parentViewController?.toast(text: err)
                
            }
            
        }
        
        
    }
    
    fileprivate lazy var anonymity_label:UILabel = {
        let control = UILabel()
        control.text = "匿名评论"
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 11, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    var beginInput:(()->())?
    
}


extension ST_RecommendVIdeoBottomView{
    
    @objc fileprivate func inputHandle(){
        
        beginInput?()
    }
    
}
